package com.mall.canal.job;

import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Darren
 * @date 2022-10-09 9:49
 * Description：
 */
@Component
public class JobCreate {
    @Autowired
    private CoordinatorRegistryCenter coordinatorRegistryCenter;

    /**
     * 定时任务创建
     * @param jobName 任务名
     * @param cron 时间表达式
     * @param shardingTotalCount 分片数量
     * @param instance 任务实例
     * @param parameter 额外参数
     */
    public void create(String jobName, String cron, int shardingTotalCount, SimpleJob instance, String parameter) {
        JobConfiguration job = JobConfiguration
                .newBuilder(jobName, shardingTotalCount)
                .cron(cron)
                .jobParameter(parameter)
                .overwrite(true)
                .build();
        new ScheduleJobBootstrap(coordinatorRegistryCenter, instance, job).schedule();
    }

}
